package com.ruoyi.douyin.monitor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.douyin.service.VideoService;
import com.ruoyi.douyin.utils.MessageUtils;
import com.ruoyi.system.domain.MonitorVideo;
import com.ruoyi.system.domain.MonitorVideoDetail;
import com.ruoyi.system.service.MonitorVideoDetailService;
import com.ruoyi.system.service.MonitorVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class MonitorVideoScheduled {
    @Autowired
    VideoService videoService;
    @Autowired
    MonitorVideoService monitorVideoService;
    @Autowired
    MonitorVideoDetailService monitorVideoDetailService;

    @Scheduled(cron = "0 0/15 * * * *")
    public void monitorVideoA() {
        List<String[]> listItem = getListItem();
        //List<String> itemIds = listItem.stream().map(item -> item[0]).collect(Collectors.toList());
        //List<String> status = listItem.stream().map(item -> item[1]).collect(Collectors.toList());
        if (listItem.size() == 0) return;
        AjaxResult result = videoService.videoDetailList(listItem);
        List data = (List) result.get("data");
        if (data == null) return;
        for (Object datum : data) {
            Map<String, Object> resultMap = JSON.parseObject(datum.toString(), new TypeReference<Map<String, Object>>() {
            }.getType());
            List resultList = ((List) ((Map) resultMap.get("data")).get("list"));
            for (Object item : resultList) {
                String shareUrl = ((Map) item).get("share_url").toString();
                String itemId = ((Map) item).get("item_id").toString();
                Map statistics = (Map) ((Map) item).get("statistics");
                String commentCount = statistics.get("comment_count").toString();
                String diggCount = statistics.get("digg_count").toString();
                String shareCount = statistics.get("share_count").toString();
                String playCount = statistics.get("play_count").toString();
                if (Integer.parseInt(diggCount) == 5000) {
                    MessageUtils.sendMessage("", shareUrl);
                }
                if (Integer.parseInt(playCount) == 5000) {
                    MessageUtils.sendMessage("", shareUrl);
                }
                if (Integer.parseInt(diggCount) == 10000) {
                    MessageUtils.sendMessage("", shareUrl);
                }
                if (Integer.parseInt(playCount) == 10000) {
                    MessageUtils.sendMessage("", shareUrl);
                }
                MonitorVideoDetail monitorVideoDetail = new MonitorVideoDetail();
                monitorVideoDetail
                        .setCommTotal(commentCount)
                        .setPlayTotal(playCount)
                        .setDiggTotal(diggCount)
                        .setItemId(itemId);
                MonitorVideoDetail monitor = monitorVideoDetailService.getOne(itemId);
                if (monitor != null) {
                    monitorVideoDetail
                            .setDiggNum((Integer.parseInt(diggCount) - Integer.parseInt(monitor.getDiggTotal())) + "")
                            .setPlayNum((Integer.parseInt(playCount) - Integer.parseInt(monitor.getPlayTotal())) + "")
                            .setCommNum((Integer.parseInt(commentCount) - Integer.parseInt(monitor.getCommTotal())) + "");
                } else {
                    monitorVideoDetail
                            .setDiggNum("0")
                            .setPlayNum("0")
                            .setCommNum("0");
                }
                System.out.println(monitorVideoDetail);
                monitorVideoDetailService.monitorVideoDetailAdd(Collections.singletonList(monitorVideoDetail));
            }
        }
        System.out.println("监控任务在执行" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    /**
     * 获取要监控的数据 from 数据库
     */
    public List<String[]> getListItem() {
        // 查询全部的数据
        List<MonitorVideo> monitorVideos = monitorVideoService.selectAll();
        return monitorVideos.stream().map(item -> new String[]{item.getItem_id(), item.getStatus().toString(), item.getOpen_id()}).collect(Collectors.toList());
    }
}
